#####################################
##REPLICATION FOR MCGHEE, ET AL.,####
##"A PRIMARY CAUSE OF PARTISANSHIP"##
#####################################

##Table A5 (Appendix)##

#Load Data#
library(Zelig)
library(sandwich)
library(MatchIt)
library(WhatIf)
setwd("XXXX") #ENTER DIRECTORY PATHNAME IN PLACE OF XXXX

legyrd <- read.csv("legislator primaries.dem.csv", header=TRUE)

#generate election year counter#
legyrd$yrcnt <- (legyrd$elec-1992)/2

#drop ideowt var, which is not used for this table#
legyrd$ideowt <- NULL

#drop missing data#
legyrd <- na.omit(legyrd)

#define years & states as factors for FE regression#
legyrd$icpsrst <- as.factor(legyrd$icpsrst)
legyrd$yrcnt <- as.factor(legyrd$yrcnt)

##Matching: Democrats##
#Semi-closed#
legyrd.semicl <- legyrd[legyrd$semicl==1 | legyrd$closed==1,]
m.out <- matchit(semicl ~ pvote + yrcnt,
	data=legyrd.semicl, method="nearest", discard="hull.both")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ semicl + distance + pvote +
	icpsrst + yrcnt, robust=list(method="vcovHAC"), 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#Semi-open
legyrd.semiop <- legyrd[legyrd$semiop==1 | legyrd$closed==1,]
m.out <- matchit(semiop ~ pvote + yrcnt,
	data=legyrd.semiop, method="nearest", discard="hull.both")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ semiop + distance + pvote +
	icpsrst + yrcnt, robust=list(method="vcovHAC"), 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#Open
legyrd.open <- legyrd[legyrd$open==1 | legyrd$closed==1,]
m.out <- matchit(open ~ pvote + yrcnt,
	data=legyrd.open, method="nearest", discard="hull.both")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ open + distance + pvote + yrcnt + icpsrst, 
	robust=list(method="vcovHAC"), 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#Nonpartisan#
legyrd.nonpart <- legyrd[legyrd$nonpart==1 | legyrd$closed==1,]
m.out <- matchit(nonpart ~ pvote + yrcnt,
	data=as.data.frame(legyrd.nonpart), method="nearest")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ nonpart + distance + pvote + icpsrst + yrcnt, 
	robust=list(method="vcovHAC"), 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)


